package com.power.sifa.approve.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.plf.common.domain.PageDataResponse;
import com.plf.common.domain.ResponseDTO;
import com.plf.common.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.power.sifa.approve.model.Apl_arrive_receipt;
import com.power.sifa.approve.model.pageInfo.Apl_arrive_receiptPageInfo;
import com.power.sifa.approve.api.IApl_arrive_receiptFeign;
import com.power.sifa.approve.service.Apl_arrive_receiptService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;



/**
 *   送达回执控制器
 */

@Slf4j
//@PowerLog
@RestController
@Api(value = "Apl_arrive_receiptController",tags={"送达回执"})
public class Apl_arrive_receiptController implements IApl_arrive_receiptFeign{

    @Resource
    private Apl_arrive_receiptService apl_arrive_receiptService;


    @Override
    @ApiOperation("查询送达回执分页列表")
    @SaCheckPermission("apl_arrive_receipt_list")
    public ResponseDTO list(Apl_arrive_receiptPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo参数不能为空");
        Page<Apl_arrive_receipt> page = this.apl_arrive_receiptService.findAll(pageInfo);
        List list = new ArrayList();
        if (!page.isEmpty()) {
            for (Apl_arrive_receipt apl_arrive_receipt : page) {
                apl_arrive_receipt.geneText();
                list.add(apl_arrive_receipt);
            }
        }
        PageDataResponse p = new PageDataResponse(list, page.getTotalElements(), page.getSize(), pageInfo.getPageNumber()<=1 ? 1 : pageInfo.getPageNumber());
        return ResponseDTO.succData(p);
    }

    @Override
    @ApiOperation("查看送达回执")
    @SaCheckPermission("apl_arrive_receipt_view")
    public ResponseDTO view(String id) {
        Assert.notNull(id, "id不能为空");
        Apl_arrive_receipt apl_arrive_receipt  = apl_arrive_receiptService.getById(id);
        if (apl_arrive_receipt != null) {
            apl_arrive_receipt.geneText();
        }
        return ResponseDTO.succData(apl_arrive_receipt);
    }

    @Override
    @ApiOperation("删除送达回执")
    @SaCheckPermission("apl_arrive_receipt_del")
    public ResponseDTO delete(String id) {
        Assert.notNull(id, "id不能为空");
        apl_arrive_receiptService.removeById(id);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation("保存或修改送达回执")
    @SaCheckPermission("apl_arrive_receipt_save")
    public ResponseDTO save(Apl_arrive_receipt apl_arrive_receipt) {
        Assert.notNull(apl_arrive_receipt, "apl_arrive_receipt不能为空");
        apl_arrive_receiptService.saveOrUpdate(apl_arrive_receipt);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation(value="批量删除送达回执")
    @SaCheckPermission("apl_arrive_receipt_del")
    public ResponseDTO batchDel(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "ids不能为空");
        List<Integer> list = Arrays.asList(ids.split(",")).stream().map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
        apl_arrive_receiptService.removeByIds(list);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation(value="导出excel")
    @SaCheckPermission("apl_arrive_receipt_list")
    public void output(HttpServletResponse response, Apl_arrive_receiptPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo不能为空");
        String title = "送达回执";
        String[] heads = {"applyid+nanoid","案号","applyid","送达记录id","发送时间","送达时间","状态"};
        String[] fields = {"id","code","applyid","recordid","sendtime","arrivetime","status"};
        Page<Apl_arrive_receipt> page = this.apl_arrive_receiptService.findAll(pageInfo);
        ExcelUtil.exportXls(title, heads, fields, page.getContent(), response);
    }

    @Override
    @ApiOperation(value="导出excel模板")
    public void exportExcelTemplate(HttpServletResponse response) {
        String title = "送达回执";
        //id自增或list从表可自行删除，需与导入项一致
        String[] heads = {"applyid+nanoid","案号","applyid","送达记录id","发送时间","送达时间","状态"};
        ExcelUtil.exportXls(title, heads, null, null, response);
    }

	@Override
	@ApiOperation(value="导入excel")
	public ResponseDTO importExcel(MultipartFile file) throws IOException {
        Assert.notNull(file, "file不能为空");
        //id自增或list从表可自行删除，需与下载的模板中导入项一致
        String[] fields = {"id","code","applyid","recordid","sendtime","arrivetime","status"};
        List<Apl_arrive_receipt> list = (List<Apl_arrive_receipt>)ExcelUtil.Import(fields, file, Apl_arrive_receipt.class);
        if(!CollectionUtils.isEmpty(list)) {
            this.apl_arrive_receiptService.saveAll(list);
        }
        return ResponseDTO.succ();
	}

    @Override
    public ResponseDTO confirmReceipt(String code, String verifycode) {
        try {
            return ResponseDTO.succData(this.apl_arrive_receiptService.confirmReceipt(code, verifycode));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("confirmReceipt 确认送达回执失败 ->  msg[{}]", e.getMessage());
            return ResponseDTO.wrap(500, "确认送达回执失败");
        }

    }

    /**
     * 查询送达回执数据
     * @param recordId 送达详情id
     * @return
     */
    @Override
    public ResponseDTO getReceiptById(String recordId) {
        try {
            return ResponseDTO.succData(this.apl_arrive_receiptService.getReceiptById(recordId));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("getReceiptById 查询送达回执数据 ->  msg[{}]", e.getMessage());
            return ResponseDTO.wrap(500, "查询送达回执数据异常");
        }
    }

}
